//
//  PersonalInforViewController.m
//  SouthChina
//
//  Created by Mr.Y on 14-10-9.
//  Copyright (c) 2014年 科创. All rights reserved.
//
#import "PersonalCenterTableViewCell.h"
#import "PersonalInforViewController.h"
#import "TakePhotoController.h"
#import "EditMobileViewController.h"
#import "EditMailViewController.h"
#import "EditHeadImgViewController.h"
#import "PersonalLabelViewController.h"
#import "selectLabelViewController.h"
#import "DataDictDao.h"
#define LABEL_HEIGHT 20
#define LABEL_WIDTH 60

@interface PersonalInforViewController()<UITableViewDataSource,UITableViewDelegate>{
    
}
@property (strong, nonatomic) IBOutlet UILabel *username;

@property (strong, nonatomic) IBOutlet UIImageView *headimage;

@property (strong, nonatomic) IBOutlet UITableView *tabview;

@property (strong, nonatomic) IBOutlet NSArray * titles; //标题列表
@property (strong, nonatomic) IBOutlet NSMutableArray * userinfo; //用户信息


@end

@implementation PersonalInforViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addTitleBarWithBackBtn:@"个人信息" needReturn:YES];
    UIView * viewBody = [[[NSBundle mainBundle] loadNibNamed:@"PersonalInforViewBody" owner:self options:nil] objectAtIndex:0];
    viewBody.frame = (CGRect){0, self.titlebar.frame.origin.y+self.titlebar.frame.size.height,SCREEN_WIDTH, SCREEN_HEIGHT-self.titlebar.frame.size.height-YNEW};
    [self.view addSubview:viewBody];
    
    _tabview.delegate = self;
    _tabview.dataSource = self;
    
    _titles = @[@[@"类型",@"部门",@"性别",@"手机",@"邮箱"],@[@"个人标签"]];
    
    //获取登陆用户名
    NSString *strusrname = [DE getGlobalVar:LOGIN_USER_NAME] == nil?@"":[DE getGlobalVar:LOGIN_USER_NAME];
    NSLog(@"%@",strusrname);
    _username.text = strusrname;
    
    //用户头像
    NSString *headStr = [DE getGlobalVar:LOGIN_USER_HEADIMG] == nil?@"":[DE getGlobalVar:LOGIN_USER_HEADIMG];
    NSURL *url= [NSURL URLWithString: [headStr hasPrefix:@"http"]?headStr:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,headStr]];
    
    [_headimage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
     _headimage.userInteractionEnabled=YES;
    _headimage.layer.masksToBounds = YES;
    _headimage.layer.cornerRadius = 23;
    UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickImage)];
    [_headimage addGestureRecognizer:singleTap];
    
   //获取学生类型（本科生等）
    NSString *usertype = [DE getGlobalVar:LOGIN_USER_TYPE] == nil?@"":[DE getGlobalVar:LOGIN_USER_TYPE];
    NSLog(@"%@",usertype);
    if([usertype isEqualToString:@"T"])
        usertype = @"教职工";
    else
        usertype = @"学生";
    //获取部门
    NSString *userdepart = [DE getGlobalVar:LOGIN_USER_DEPT] == nil?@"":[DE getGlobalVar:LOGIN_USER_DEPT];
    NSLog(@"%@",userdepart);
    //获取性别
    NSString *usersex = [DE getGlobalVar:LOGIN_USER_SEX] == nil?@"":[DE getGlobalVar:LOGIN_USER_SEX];
    NSLog(@"%@",usersex);
    //获取手机号
    NSString *usertelphone = [DE getGlobalVar:LOGIN_USER_MOBILE] == nil?@"":[DE getGlobalVar:LOGIN_USER_MOBILE];
    NSLog(@"%@",usertelphone);
    //邮箱
    NSString *usermail = [DE getGlobalVar:LOGIN_USER_EMAIL] == nil?@"":[DE getGlobalVar:LOGIN_USER_EMAIL];
    NSLog(@"%@",usermail);
    
    NSArray *userinfocurr = @[usertype,userdepart,usersex,usertelphone,usermail];
   _userinfo = [[NSMutableArray alloc] initWithArray:userinfocurr];
    
}

//返回分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

//每个分区多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section==0)
        return 5;
    else
        return 1;
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 25;
}

//设置每行的显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"PersonalCenterTableViewCell";
    PersonalCenterTableViewCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
            }
    cell.titleLabel.text = _titles[indexPath.section][indexPath.row];
    if(indexPath.section!=1)
    {
        if(![_userinfo[indexPath.row] isEqualToString:@""])
        {
        UILabel *_userinfolabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.titleLabel.frame.origin.x+100,cell.titleLabel.frame.origin.y+12,186, 20)];
        _userinfolabel.text = _userinfo[indexPath.row];
        [_userinfolabel setTextAlignment:NSTextAlignmentRight];
        _userinfolabel.font = [UIFont systemFontOfSize:16];
        if(indexPath.row>=0 && indexPath.row<=2)
            _userinfolabel.textColor = [UIColor colorWithRed:169.0/255 green:169.0/255 blue:169.0/255 alpha:1];
        else
            _userinfolabel.textColor = [UIColor colorWithRed:100.0/255 green:149.0/255 blue:237.0/255 alpha:1];
        [cell addSubview:_userinfolabel];
        }
        
    }
    else
    {
      //加箭头
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }
    
    return cell;
}

-(void) onClickImage
{
    
    NSLog(@"点击图片事件");
    EditHeadImgViewController * editmobileview = [[EditHeadImgViewController alloc]init];
    [kAppDelegate.iCSDrawerController.navigationController pushViewController:editmobileview animated:NO ];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"select row");
    if(indexPath.section==0){
        switch (indexPath.row) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                break;
            case 3: //手机
            {  EditMobileViewController * editmobileview = [[EditMobileViewController alloc]init];
                [kAppDelegate.iCSDrawerController.navigationController pushViewController:editmobileview animated:NO ];}
                break;
            case 4: //邮箱
            {  EditMailViewController * editmobileview = [[EditMailViewController alloc]init];
                [kAppDelegate.iCSDrawerController.navigationController pushViewController:editmobileview animated:NO ];}
                break;
                
            default:
                break;
        }
    }
    else{  //个人标签
        /*PersonalLabelViewController * personalLabel = [[PersonalLabelViewController alloc]init];
        [kAppDelegate.iCSDrawerController.navigationController pushViewController:personalLabel animated:NO ];*/
        //暂时没有添加个人标签功能
        selectLabelViewController * select = [[selectLabelViewController alloc] init];
         NSMutableSet *selectedLable = [[NSMutableSet alloc]init];
         NSString *sinittags = [DE getGlobalVar:LOGIN_USER_TAGS] == nil?@"":[DE getGlobalVar:LOGIN_USER_TAGS];
        if(sinittags!=nil)
        {
           [selectedLable addObjectsFromArray:[self getlabelDictArr:sinittags]];
        }
        select.selectedLable  = selectedLable;
        select.sLableType= PERSON_LABLE;
        [kAppDelegate.iCSDrawerController.navigationController pushViewController:select animated:NO];
        }
}

//根据dictdataId获取DicData
-(NSMutableArray *) getlabelDictArr:(NSString *)dictdataId
{
    NSMutableArray *ArrList = [[NSMutableArray alloc] init];
    NSArray *list=[dictdataId componentsSeparatedByString:@","];
    NSMutableString *skeylist = [[NSMutableString alloc] initWithString:@""];
    for(NSInteger i = 0;i<list.count;i++)
    {
        if(![list[i] isEqualToString:@""])
        {
           if(i==0)
             [skeylist insertString:[NSString stringWithFormat:@"'%@'",list[i]] atIndex:0];
           else
             [skeylist insertString:[NSString stringWithFormat:@"'%@',",list[i]] atIndex:0];
        }
    }
    NSString *sql = [NSString stringWithFormat:@"where dicttypeId ='8' and dictdataValue in (%@)",skeylist];
    ArrList = [DataDictDao queryDataDict:sql];
        
    return  ArrList;
}



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    //刷新参数手机号码
    NSString *usertelphone = [DE getGlobalVar:LOGIN_USER_MOBILE] == nil?@"":[DE getGlobalVar:LOGIN_USER_MOBILE];
    [_userinfo replaceObjectAtIndex:3 withObject:usertelphone];
    //刷新参数邮箱
    NSString *usermail = [DE getGlobalVar:LOGIN_USER_EMAIL] == nil?@"":[DE getGlobalVar:LOGIN_USER_EMAIL];
    [_userinfo replaceObjectAtIndex:4 withObject:usermail];
    //刷新用户名
    NSString *strusrname = [DE getGlobalVar:LOGIN_USER_NAME] == nil?@"":[DE getGlobalVar:LOGIN_USER_NAME];
    NSLog(@"%@",strusrname);
    _username.text = strusrname;
    
    //用户头像
    NSString *headStr = [DE getGlobalVar:LOGIN_USER_HEADIMG] == nil?@"":[DE getGlobalVar:LOGIN_USER_HEADIMG];
    NSURL *url= [NSURL URLWithString: [headStr hasPrefix:@"http"]?headStr:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,headStr]];
    
    [_headimage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    
     //刷新列表
    [_tabview reloadData];
}

//重写提示效果(个性化)
-(void)showAlerHUD:(NSString*)text
{
    BDKNotifyHUD * hudex = [[BDKNotifyHUD alloc] initWithImageEX:[UIImage imageNamed:@""]  text:text hudWidth:180.0f];
    hudex.center = CGPointMake(self.view.center.x,SCREEN_HEIGHT-50);
    [self.view addSubview:hudex];
    [hudex presentWithDuration:1.5 speed:0.5f inView:self.view completion:^{
        [hudex removeFromSuperview];
    }];
}

- (void)btnBack {
    kAppDelegate.needRefrsh = YES;
    [super btnBack];
}



@end
 


